package com.heima.behavior.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.common.constants.message.FollowBehaviorConstants;
import com.heima.model.behavior.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author TheCai
 * @date 2021/6/4 20:31
 */
@Component
@Slf4j
public class FollowBehaviorListener {

  @Autowired ApFollowBehaviorService apFollowBehaviorService;

  @KafkaListener(topics = FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC)
  public void followBehaviorListener(String msg) {
    if (StrUtil.isNotEmpty(msg)) {
      log.info("===========" + "FollowBehaviorListener receiverMessage：{}", msg);
    }
    FollowBehaviorDto dto = JSONUtil.toBean(msg, FollowBehaviorDto.class);
    // save 到关注计次表中
    apFollowBehaviorService.saveFollowBehavior(dto);
    log.info("===========FollowBehaviorListener success===========");
  }
}
